import tkinter as tk
from tkinter import font, END, ANCHOR, filedialog, ttk
from PIL import Image, ImageTk
from logic.function import load_tasks, add_task, delete_task, edit_task, get_tasks  # 使用相对路径导入逻辑模块中的函数
from tkcalendar import DateEntry  # 导入tkcalendar中的DateEntry
from .kanban import create_task, update_kanban, move_task, setup_kanban  # 使用相对路径导入看板设置函数

def main_window():
    # UI相关设置
    default_font = ("Times", 20)

    # 定义函数
    def add_task_ui():
        task = entry_task.get()
        priority = priority_var.get()
        description = entry_description.get()
        date = cal.get_date()  # 获取选定的日期
        if task:
            add_task(priority, description, date, task)
            listbox_tasks.insert(END, f"{priority} | {description} | {date} | {task}")
        entry_task.delete(0, END)
        entry_description.delete(0, END)

    def delete_task_ui():
        selected_index = listbox_tasks.curselection()
        if selected_index:
            index = selected_index[0]
            delete_task(index)
            listbox_tasks.delete(index)

    def edit_task_ui():
        selected_index = listbox_tasks.curselection()
        if selected_index:
            index = selected_index[0]
            _, description, date, task = get_tasks()[index].split(' | ')
            
            # 清除当前输入框内容
            entry_task.delete(0, END)
            entry_description.delete(0, END)
            
            # 插入新的内容
            entry_task.insert(0, task)
            entry_description.insert(0, description)
            cal.set_date(date)  # 设置日期选择器为当前任务的日期
            priority_var.set(_)
            
            # 删除旧的任务条目
            listbox_tasks.delete(index)
            
            # 更新任务列表
            new_priority = priority_var.get()
            new_description = entry_description.get()
            new_date = cal.get_date()
            new_task = entry_task.get()
            
            # 调用 edit_task 函数
            edit_task(index, new_priority, new_description, new_date, new_task)
            
            # 重新插入更新后的任务
            listbox_tasks.insert(index, f"{new_priority} | {new_description} | {new_date} | {new_task}")

    def add_photo():
        global image_path
        image_path = filedialog.askopenfilename(title="choose a photo", filetypes=(("Image Files", "*.png;*.jpg;*.jpeg;*.gif;*.bmp"), ("All Files", "*.*")))
        if image_path:
            print(f"Selected image path: {image_path}")  
            display_image(image_path)
        else:
            print("No image selected")

    def display_image(path):
        try:
            img = Image.open(path)
            print(f"Image opened successfully, size: {img.size}")  
            img.thumbnail((200, 200))
            img_tk = ImageTk.PhotoImage(img)
            label_image.config(image=img_tk)
            label_image.image = img_tk  
        except Exception as e:
            print(f"Error loading image: {e}")

    # 初始化UI
    window = tk.Tk()
    window.title("To-Do List")

    # 定义UI元素
    generaltitle = tk.Label(window, text="Title:", font=default_font)
    generaltitle.grid(row=0, column=0, padx=5, pady=5)

    entry_task = tk.Entry(window, width=30, font=default_font)
    entry_task.grid(row=0, column=1, padx=5, pady=5)

    label_priority = tk.Label(window, text="Priority:", font=default_font)
    label_priority.grid(row=1, column=0, padx=5, pady=5)

    priority_var = tk.StringVar(value="Low")
    option_menu_priority = tk.OptionMenu(window, priority_var, "Low", "Medium", "High")
    option_menu_priority.grid(row=1, column=1, padx=5, pady=5)
    option_menu_priority.config(font=("Times", 20))

    label_description = tk.Label(window, text="Description:", font=default_font)
    label_description.grid(row=2, column=0, padx=5, pady=5)

    entry_description = tk.Entry(window, width=30, font=default_font)
    entry_description.grid(row=2, column=1, padx=5, pady=5)

    label_date = tk.Label(window, text="Date: ", font=default_font)
    label_date.grid(row=3, column=0, padx=5, pady=5)

    cal = DateEntry(window, selectmode='day', date_pattern='yyyy-mm-dd', font=default_font)
    cal.grid(row=3, column=1, padx=5, pady=5)

    label_image = tk.Label(window, borderwidth=2, relief="solid")
    label_image.grid(row=4, column=0, columnspan=2, padx=10, pady=10, sticky="nsew")

    listbox_title = tk.Label(window, text="List:", font=default_font)
    listbox_title.grid(row=4, column=0, padx=5, pady=5)

    listbox_tasks = tk.Listbox(window, height=5, width=40, font=default_font)
    listbox_tasks.grid(row=5, column=0, columnspan=2, padx=5, pady=5)

    button_add_task = tk.Button(window, text="Add", font=default_font, command=add_task_ui)
    button_add_task.grid(row=6, column=0, padx=5, pady=5)

    button_delete_task = tk.Button(window, text="Delete", font=default_font, command=delete_task_ui)
    button_delete_task.grid(row=6, column=1, padx=5, pady=5)

    button_edit_task = tk.Button(window, text="Edit", font=default_font, command=edit_task_ui)
    button_edit_task.grid(row=7, column=0, padx=5, pady=5)

    button_add_photo = tk.Button(window, text="Add picture", font=default_font, command=add_photo)
    button_add_photo.grid(row=7, column=1, padx=5, pady=5)

    for i in range(9):  
        window.grid_rowconfigure(i, weight=1)
    window.grid_columnconfigure(0, weight=1)
    window.grid_columnconfigure(1, weight=1)

    # 加载初始任务
    load_tasks()  # 确保在显示UI之前加载任务
    for task in get_tasks():
        listbox_tasks.insert(END, task)
    
    def on_listbox_select(event):
        selected_item = listbox_tasks.get(tk.ACTIVE)
        print(f'You selected: {selected_item}')

    # 看板UI
    def open_kanban_board():
        kanban_window = tk.Toplevel(window)
        kanban_window.title("Board")
        kanban_window.geometry('1800x300+0+150')
        
        # 初始化看板
        setup_kanban(kanban_window)

      # 创建一个新的 Frame 来管理按钮
    button_frame = tk.Frame(window)
    button_frame.grid(row=8, column=0, columnspan=2, pady=5)

    button_open_kanban = tk.Button(button_frame, text="Kanban", command=open_kanban_board, font=default_font, relief='flat', width=10)
    button_open_kanban.pack()

    for i in range(9):  
        window.grid_rowconfigure(i, weight=1)
    window.grid_columnconfigure(0, weight=1)
    window.grid_columnconfigure(1, weight=1)

    # 加载初始任务
    load_tasks()  # 确保在显示UI之前加载任务
    for task in get_tasks():
        listbox_tasks.insert(END, task)
    
    def on_listbox_select(event):
        selected_item = listbox_tasks.get(tk.ACTIVE)
        print(f'You selected: {selected_item}')    # 启动窗口
    window.mainloop()

if __name__ == "__main__":
    main_window()
    
    
